package CustomizeInterface

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type CxLengthLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCxLengthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CxLengthLogic {
	return &CxLengthLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// CxLength 是一个处理长度判断逻辑的方法
// 它接收一个判断条件请求，并返回一个响应或错误
func (l *CxLengthLogic) CxLength(req *types.JudgmentConditions) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 调用 CustomizeService 的 CxLength 方法处理业务逻辑
	res, err := l.svcCtx.CustomizeService.CxLength(l.ctx, &property_service.JudgmentConditions{
		TableName:       req.TableName,
		JudgmentField:   req.JudgmentField,
		JudgmentValue:   req.JudgmentValue,
		AssignmentField: req.AssignmentField,
		Assignment:      req.Assignment,
		TenantID:        tenantID,
	})
	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}
	// 构建成功响应
	resp, err = result.Suc(res).End()
	//获取调用返回的id，并将其封装到响应数据中
	resp.Data = map[string]any{
		"length": res.Id,
	}
	// 返回成功的响应
	return resp, err
}
